/***************************************************************
 * Name:      xgsof2textMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    walnutcy (walnutcy@gmail.com)
 * Created:   2012-09-12
 * Copyright: walnutcy (weibo.com/walnutcy)
 * License:
 **************************************************************/

#include "xgsof2textMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(xgsof2textDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)
#include "stdef.h"
#include <wx/wx.h>



xgsof2textDialog*  p_globalDlg = NULL;
void wxAfxLogFmt(u8 isThread, const wxChar* fmt, ...)
{
    if (NULL==p_globalDlg) return;

    wxChar  buffer[1024];
    va_list va;

    va_start(va, fmt);
    wxVsnprintf(buffer, 1024, fmt, va);
    va_end(va);

    if (isThread)
    {
        wxMutexGuiEnter();
        p_globalDlg->LogTxt(buffer);
        wxMutexGuiLeave();
    }
    else
    {
        p_globalDlg->LogTxt(buffer);
    }
}






//helper functions
enum wxbuildinfoformat
{
    short_f, long_f
};

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(xgsof2textDialog)
const long xgsof2textDialog::ID_BUTTON_LOAD = wxNewId();
const long xgsof2textDialog::ID_CHOICE_TYPE = wxNewId();
const long xgsof2textDialog::ID_CHECKBOX_ASCII = wxNewId();
const long xgsof2textDialog::ID_BUTTON_CONVERT = wxNewId();
const long xgsof2textDialog::ID_STATICLINE1 = wxNewId();
const long xgsof2textDialog::ID_BUTTON_ABOUT = wxNewId();
const long xgsof2textDialog::ID_BUTTON_QUIT = wxNewId();
const long xgsof2textDialog::ID_STATICTEXT_LOADPATH = wxNewId();
const long xgsof2textDialog::ID_STATICTEXT_SAVEPATH = wxNewId();
const long xgsof2textDialog::ID_TEXTCTRL_TIPSINFO = wxNewId();
//*)

BEGIN_EVENT_TABLE(xgsof2textDialog,wxDialog)
    //(*EventTable(xgsof2textDialog)
    //*)
END_EVENT_TABLE()

xgsof2textDialog::xgsof2textDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(xgsof2textDialog)
    wxBoxSizer* BoxSizer3;

    Create(parent, wxID_ANY, _("xgsof2text"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    m_Btt_Load = new wxButton(this, ID_BUTTON_LOAD, _("Load ..."), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON_LOAD"));
    BoxSizer2->Add(m_Btt_Load, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    m_Choice_Type = new wxChoice(this, ID_CHOICE_TYPE, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE_TYPE"));
    m_Choice_Type->SetSelection( m_Choice_Type->Append(_("GSOF")) );
    BoxSizer2->Add(m_Choice_Type, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    cb_FlgASCII = new wxCheckBox(this, ID_CHECKBOX_ASCII, _("ASCII"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX_ASCII"));
    cb_FlgASCII->SetValue(false);
    BoxSizer2->Add(cb_FlgASCII, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    m_Btt_Convert = new wxButton(this, ID_BUTTON_CONVERT, _("Convert ..."), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON_CONVERT"));
    BoxSizer2->Add(m_Btt_Convert, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    StaticLine1 = new wxStaticLine(this, ID_STATICLINE1, wxDefaultPosition, wxSize(10,-1), wxLI_HORIZONTAL, _T("ID_STATICLINE1"));
    BoxSizer2->Add(StaticLine1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    m_Btt_About = new wxButton(this, ID_BUTTON_ABOUT, _("About"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON_ABOUT"));
    BoxSizer2->Add(m_Btt_About, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    m_Btt_Quit = new wxButton(this, ID_BUTTON_QUIT, _("Quit"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON_QUIT"));
    BoxSizer2->Add(m_Btt_Quit, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    BoxSizer1->Add(BoxSizer2, 0, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    m_FilePath_Load = new wxStaticText(this, ID_STATICTEXT_LOADPATH, _("Open File ..."), wxDefaultPosition, wxSize(357,20), wxALIGN_LEFT|wxSTATIC_BORDER, _T("ID_STATICTEXT_LOADPATH"));
    BoxSizer3->Add(m_FilePath_Load, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    m_FilePath_Save = new wxStaticText(this, ID_STATICTEXT_SAVEPATH, _("Save File ..."), wxDefaultPosition, wxSize(357,20), wxALIGN_LEFT|wxSTATIC_BORDER, _T("ID_STATICTEXT_SAVEPATH"));
    BoxSizer3->Add(m_FilePath_Save, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    m_TipsInfo = new wxTextCtrl(this, ID_TEXTCTRL_TIPSINFO, wxEmptyString, wxDefaultPosition, wxSize(573,122), wxTE_MULTILINE|wxTE_READONLY|wxTE_LEFT|wxVSCROLL, wxDefaultValidator, _T("ID_TEXTCTRL_TIPSINFO"));
    BoxSizer3->Add(m_TipsInfo, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    BoxSizer1->Add(BoxSizer3, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
    SetSizer(BoxSizer1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_BUTTON_LOAD,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&xgsof2textDialog::Onm_Btt_LoadClick);
    Connect(ID_BUTTON_CONVERT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&xgsof2textDialog::Onm_Btt_ConvertClick);
    Connect(ID_BUTTON_ABOUT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&xgsof2textDialog::OnAbout);
    Connect(ID_BUTTON_QUIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&xgsof2textDialog::OnQuit);
    //*)


    p_globalDlg = this;
}

xgsof2textDialog::~xgsof2textDialog()
{
    //(*Destroy(xgsof2textDialog)
    //*)
}

void xgsof2textDialog::OnQuit(wxCommandEvent& event)
{
    Close();
}

void xgsof2textDialog::OnAbout(wxCommandEvent& event)
{
    wxString msg;
    msg  = _("xgsof2text ver1.0\r\n\r\n");
    msg += _("++Author: walnutcy\r\n");
    msg += _("++Email : walnutcy@gmail.com\r\n");
    msg += _("++Build : ");
    msg += _(__DATE__);
    msg += _(" ");
    msg += _(__TIME__);
    msg += _("\r\n\r\nLIB:");
    msg += wxbuildinfo(long_f);
    wxMessageBox(msg, _("About"));
}

void xgsof2textDialog::LogTxt(wxString str)
{
    m_TipsInfo->AppendText(str);
    TipsCtrl();
}

void xgsof2textDialog::TipsCtrl(void)
{
    int     i;
    long    len;
    int     lineNo = m_TipsInfo->GetNumberOfLines();

    if (lineNo < 30)    return;

    len = 0;
    for(i=0; i<20; i++)
    {
        len += m_TipsInfo->GetLineLength(i);
    }
    m_TipsInfo->Remove(0, len);
}

void xgsof2textDialog::Onm_Btt_LoadClick(wxCommandEvent& event)
{
    m_filePath = wxFileSelector( wxT("Load GSOF RAW Data"), (const wxChar *)NULL,
                                 (const wxChar *)NULL,
                                 wxT("*"), wxT("GSOF RAW Data (*.*)|*.*") );

    if (m_filePath.empty())  return;

    m_FilePath_Load->SetLabel(m_filePath);
}

void xgsof2textDialog::Onm_Btt_ConvertClick(wxCommandEvent& event)
{
    wxString str;

    if (m_filePath.empty())  return;

    str = m_filePath;
    str += _(".log");
    m_FilePath_Save->SetLabel(str);

    m_TipsInfo->Clear();
    mFlg_reload = cb_FlgASCII->GetValue();

    p_ThreadFile = new proThread(this);
    if (p_ThreadFile->Create() != wxTHREAD_NO_ERROR)
    {
        LogTxt(_("Cant create thread!\r\n"));
        return;
    }
    p_ThreadFile->Run();

}
